package org.example.gui;

import lombok.Getter;
import lombok.Setter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

/**
 * 控制面板
 */
@Setter
@Getter
class PicturePanel extends JPanel implements ActionListener {
    /**
     * 保存,复制,关闭的按钮
     */
    private JButton save, copy, close;
    /**
     * 得到的图片
     */
    private BufferedImage get;
    private CaptureScreen screen;

    public PicturePanel(BufferedImage get, CaptureScreen screen) {
        super(new BorderLayout());
        this.screen = screen;
        this.get = get;
        initPanel();
    }

    private void initPanel() {
        copy = new JButton("复制到剪帖板(C)");
        close = new JButton("关闭(X)");
        save = new JButton("保存(S)");
        save.setMnemonic('S');
        copy.setMnemonic('C');
        close.setMnemonic('X');
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(copy);
        buttonPanel.add(save);
        buttonPanel.add(close);
        JLabel icon = new JLabel(new ImageIcon(get));
        this.add(new JScrollPane(icon), BorderLayout.CENTER);
        this.add(buttonPanel, BorderLayout.SOUTH);
        save.addActionListener(this);
        copy.addActionListener(this);
        close.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == save) {
            screen.doSave(get);
        } else if (source == copy) {
            screen.doCopy(get);
        } else if (source == close) {
            get = null;
            screen.doClose(this);
        }
    }

}